using System;
using System.Runtime.Serialization;

namespace BanZuiCloudControlAgent.Utils.Exceptions
{
    /// <summary>
    /// 网络相关异常类，用于表示网络连接、通信等过程中发生的错误
    /// </summary>
    [Serializable]
    public class NetworkException : BanZuiException
    {
        /// <summary>
        /// 网络错误类型
        /// </summary>
        public NetworkErrorType ErrorType { get; }

        /// <summary>
        /// 初始化NetworkException类的新实例
        /// </summary>
        public NetworkException() : base("E100", "Network", "发生网络错误")
        {
            ErrorType = NetworkErrorType.Unknown;
        }

        /// <summary>
        /// 使用指定的错误消息初始化NetworkException类的新实例
        /// </summary>
        /// <param name="message">描述错误的消息</param>
        public NetworkException(string message) : base("E100", "Network", message)
        {
            ErrorType = NetworkErrorType.Unknown;
        }

        /// <summary>
        /// 使用指定的错误消息和内部异常初始化NetworkException类的新实例
        /// </summary>
        /// <param name="message">描述错误的消息</param>
        /// <param name="innerException">导致当前异常的异常</param>
        public NetworkException(string message, Exception innerException) : base("E100", "Network", message, false, innerException)
        {
            ErrorType = NetworkErrorType.Unknown;
        }

        /// <summary>
        /// 使用指定的错误类型和消息初始化NetworkException类的新实例
        /// </summary>
        /// <param name="errorType">网络错误类型</param>
        /// <param name="message">描述错误的消息</param>
        public NetworkException(NetworkErrorType errorType, string message) : base(GetErrorCode(errorType), "Network", message)
        {
            ErrorType = errorType;
        }

        /// <summary>
        /// 使用指定的错误类型、消息和内部异常初始化NetworkException类的新实例
        /// </summary>
        /// <param name="errorType">网络错误类型</param>
        /// <param name="message">描述错误的消息</param>
        /// <param name="innerException">导致当前异常的异常</param>
        public NetworkException(NetworkErrorType errorType, string message, Exception innerException) : base(GetErrorCode(errorType), "Network", message, false, innerException)
        {
            ErrorType = errorType;
        }

        /// <summary>
        /// 使用序列化数据初始化NetworkException类的新实例
        /// </summary>
        /// <param name="info">SerializationInfo，它存有关于所引发的异常的序列化的对象数据</param>
        /// <param name="context">包含有关源或目标的上下文信息</param>
        protected NetworkException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
            ErrorType = (NetworkErrorType)info.GetInt32("ErrorType");
        }

        /// <summary>
        /// 使用可序列化的数据填充SerializationInfo
        /// </summary>
        /// <param name="info">要填充数据的SerializationInfo</param>
        /// <param name="context">此序列化的目标</param>
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("ErrorType", (int)ErrorType);
        }

        /// <summary>
        /// 根据网络错误类型获取错误代码
        /// </summary>
        /// <param name="errorType">网络错误类型</param>
        /// <returns>错误代码</returns>
        private static string GetErrorCode(NetworkErrorType errorType)
        {
            switch (errorType)
            {
                case NetworkErrorType.ConnectionFailed:
                    return "E101";
                case NetworkErrorType.Timeout:
                    return "E102";
                case NetworkErrorType.InvalidResponse:
                    return "E103";
                case NetworkErrorType.ServerError:
                    return "E104";
                case NetworkErrorType.AuthenticationFailed:
                    return "E105";
                default:
                    return "E100";
            }
        }
    }

    /// <summary>
    /// 网络错误类型枚举
    /// </summary>
    public enum NetworkErrorType
    {
        /// <summary>
        /// 未知错误
        /// </summary>
        Unknown = 0,

        /// <summary>
        /// 连接失败
        /// </summary>
        ConnectionFailed = 1,

        /// <summary>
        /// 连接超时
        /// </summary>
        Timeout = 2,

        /// <summary>
        /// 无效响应
        /// </summary>
        InvalidResponse = 3,

        /// <summary>
        /// 服务器错误
        /// </summary>
        ServerError = 4,

        /// <summary>
        /// 认证失败
        /// </summary>
        AuthenticationFailed = 5
    }
} 